package com.dlb.andorid;


import android.util.Log;

import com.dlb.andorid.bean.ArgAddAddress;
import com.dlb.andorid.bean.ArgAddOrder;
import com.dlb.andorid.bean.ArgMerchantReg;
import com.dlb.andorid.bean.ArgPayOrder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kwan.xframe.mvp.model.api.BaseAPIUtil;

import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Flowable;

public class XApiUtil extends BaseAPIUtil {


	private String mBaseUrl;
	private String mTokenUrl;
	private String mUpLoadUrl;
	private String mToken;

	public XApiUtil(String mBaseUrl, String mTokenUrl, String mUpLoadUrl, String mToken) {
		this.mBaseUrl = mBaseUrl;
		this.mTokenUrl = mTokenUrl;
		this.mUpLoadUrl = mUpLoadUrl;
		this.mToken = mToken;
	}

	@Override
	protected String getBaseUrl() {
		return mBaseUrl;
	}

	@Override
	protected String getBaseTokenUrl() {
		return mTokenUrl;
	}

	@Override
	protected String getBaseUpLoadUrl() {
		return mUpLoadUrl;
	}

	@Override
	protected String getToken() {
		return mToken;
	}


	Flowable<String> doServerAPI(Map args) {
		Log.e("kwan", args.toString());
		return serverAPI(TYPE_JSON).create(XAPI.class).getData(args);
	}

	Flowable<String> doServerAPIPost(Map args) {
		Log.e("kwan", args.toString());
		return serverAPI(TYPE_JSON).create(XAPI.class).getDataPost(args);
	}

	Flowable<String> doServerAPIPost(Map args, String body) {
		Log.e("kwan", args.toString());
		return serverAPI(TYPE_JSON).create(XAPI.class).getDataPostBody(args, body);
	}


	public Flowable<String> getBanner() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "rotation_pic");
		return doServerAPI(args);
	}

	public Flowable<String> getSecType(long id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "classname");
		args.put("interface", "{\"class_id\":" + id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getProducts(long id, int page, int num) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "commodity_list");
		args.put("interface", "{\"commodity_id\":1,\"type_id\":" + id + ",\"page\":" + page + ",\"num\":" + num + "} ");
		return doServerAPI(args);
	}

	public Flowable<String> getProductDetail(long id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "commodity_info");
		args.put("interface", "{\"id\":" + id + ",\"user_id\":" + App.mUserInfo.getUid() + "}");
		return doServerAPI(args);
	}

	public Flowable<String> addCar(long id, String userId, int num, long typeId, float price) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "shopping_cart");
		Log.e("kwan", "addcar:" + id);
		String arg = "{\"user_id\":" + userId + ",\"commodity_id\":" + id + ",\"num\":" + num + ",\"type_id\":" + typeId + ",\"sale_price\":\"" + price + "\"}";
		Log.e("kwan", "addcar:" + arg);
		args.put("interface", arg);

		return doServerAPI(args);
	}

	public Flowable<String> getCar(String userId) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "shopping_cart_list");
		args.put("interface", "{\"user_id\":" + userId + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getCode(String phone, int type) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "code");
		args.put("interface", "{\"phone\":" + phone + ",\"type\":" + type + "}");
		return doServerAPI(args);
	}

	public Flowable<String> login(String arg) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "user_login");
		args.put("interface", arg);
		Log.e("kwan", "login:" + arg);
		return doServerAPI(args);
	}

	public Flowable<String> reg(String arg) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "user_reg");
		args.put("interface", arg);
		return doServerAPI(args);
	}

	public Flowable<String> findPwd(String code, String phone, String password) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "password");
		args.put("interface", "{\"code\":\"" + code + "\",\"phone\":\"" + phone + "\",\"password\":\"" + password + "\"}");
		return doServerAPI(args);
	}


//	十、搜索查询
//	index.php?action=commodity_search&interface={"name":"","merchant_id":1}
//	action	commodity_search
//	interfac	{"name":"","merchant_id":1}
//	返回	json字符串
//	解析出来后的内容信息同首页商品列表


//	十一、推荐商品
//	index.php?action=recommend_list
//	action	recommend_list
//	返回	json字符串
//	解析出来后的内容信息同首页商品列表

	public Flowable<String> getRecommendProducts() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "recommend_list");
		return doServerAPI(args);
	}

	public Flowable<String> searchProducts(String txt, long merchant_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "commodity_search");
		args.put("interface", "{\"name\":\"" + txt + "\",\"merchant_id\":" + merchant_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> editCarNum(long id, int num) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "shopping_cart_status");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"data\":{\"shopping_cart_id\":" + id + ",\"num\":" + num + "}}");
		return doServerAPI(args);
	}

	public Flowable<String> getJrbk(long merchant_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "hot_commodity");
		//args.put("interface", "{\"merchant_id\":" + merchant_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> addOrder(ArgAddOrder cars) {

		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order_add");
		Gson gson = new Gson();
		String json = gson.toJson(cars, new TypeToken<ArgAddOrder>() {
		}.getType());
		Log.e("kwan", "add order:" + gson);
		args.put("interface", json);
		return doServerAPI(args);

	}

	public Flowable<String> payOrder(ArgPayOrder arg) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order_pay");
		Gson gson = new Gson();
		String json = gson.toJson(arg, new TypeToken<ArgPayOrder>() {
		}.getType());
		Log.e("kwan", "payOrder:" + gson);
		args.put("interface", json);
		return doServerAPI(args);
	}

	public Flowable<String> getOrderState(ArgPayOrder arg) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order_status");
		Gson gson = new Gson();
		String json = gson.toJson(arg, new TypeToken<ArgPayOrder>() {
		}.getType());
		args.put("interface", json);
		return doServerAPI(args);
	}

	public Flowable<String> addAddress(ArgAddAddress arg) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "add_address");
		Gson gson = new Gson();
		String json = gson.toJson(arg, new TypeToken<ArgAddAddress>() {
		}.getType());
		args.put("interface", json);
		return doServerAPI(args);
	}

	public Flowable<String> editAddress(ArgAddAddress arg) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "edit_address");
		Gson gson = new Gson();
		String json = gson.toJson(arg, new TypeToken<ArgAddAddress>() {
		}.getType());
		args.put("interface", json);
		return doServerAPI(args);
	}

	public Flowable<String> getAddress() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "address_list");
		args.put("interface", "{\"user_id\":\"" + App.mUserInfo.getUid() + "\"}");
		return doServerAPI(args);
	}

	public Flowable<String> getCity() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "city");
		return doServerAPI(args);
	}

	public Flowable<String> getCouponList() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "coupon_list");
		args.put("interface", "{\"user_id\":\"" + App.mUserInfo.getUid() + "\"}");
		return doServerAPI(args);
	}

	public Flowable<String> getAddressFee(long province_id, long city_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "address_total_fee");
		args.put("interface", "{\"province_id\":" + province_id + ",\"city_id\":" + city_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getZbhdList(long address_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "periphery_shop");
		args.put("interface", "{\"address_id\":" + address_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getAddressRetrieval(long province_id, long city_id, long area_id, String keyword) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "address_retrieval");
		args.put("interface", "{\"province_id\":" + province_id + ",\"city_id\":" + city_id + ",\"area_id\":" + area_id + ",\"keyword\":\"" + keyword + "\"}");
		return doServerAPI(args);
	}


	public Flowable<String> getMerchantType() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "merchant_type");
		return doServerAPI(args);
	}

	public Flowable<String> getMerchantReg(ArgMerchantReg arg) {

		Gson gson = new Gson();
		String json = gson.toJson(arg, new TypeToken<ArgMerchantReg>() {
		}.getType());
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "merchant_reg");
		//args.put("interface", json);
		return doServerAPIPost(args, json);
	}

	public Flowable<String> getRiderReg(ArgMerchantReg arg) {

		Gson gson = new Gson();
		String json = gson.toJson(arg, new TypeToken<ArgMerchantReg>() {
		}.getType());

		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "rider_reg");
		args.put("interface", json);
		return doServerAPI(args);
	}

	public Flowable<String> getZbhdInfo(long periphery_shop_id, int page) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "periphery_shop_info");
		args.put("interface", "{\"periphery_shop_id\":" + periphery_shop_id + ",\"page\":" + page + "}");
		return doServerAPI(args);
	}

	public Flowable<String> recharge(int type, float recharge_money) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "recharge");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"type\":" + type + ",\"recharge_money\":" + recharge_money + ",\"pay_system\":1}");
		return doServerAPI(args);
	}

	public Flowable<String> exchangePoint(long exchange_num) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "integral_exchange");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"exchange_num\":" + exchange_num + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getUserInfo() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "user_info");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getUserAccount() {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "user_account");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + "}");
		return doServerAPI(args);
	}


	public Flowable<String> getOrderList(int type) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"type\":" + type + "}");
		return doServerAPI(args);
	}


	public Flowable<String> delOrder(long orderId, String orderNo) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "del_order");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"order_id\":" + orderId + ",\"order_no\":\"" + orderNo + "\"}");
		return doServerAPI(args);
	}

	public Flowable<String> getOrderInfo(String orderNo) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order_info");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"order_no\":\"" + orderNo + "\"}");
		return doServerAPI(args);
	}

	//index.php?action=order_tk&interface={“user_id”:100010,”order_id”:100001,”order_no”:”DLB_1573455481_4476108208”}
	public Flowable<String> tkOrder(long orderId, String orderNo) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order_tk");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"order_id\":" + orderId + ",\"order_no\":\"" + orderNo + "\"}");
		return doServerAPI(args);
	}

	public Flowable<String> pjOrder(long orderId, String orderNo) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "order_evaluate");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"order_id\":" + orderId + ",\"order_no\":\"" + orderNo + "\"}");
		return doServerAPI(args);
	}


	public Flowable<String> deleteAddress(long address_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "del_address");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"address_id\":" + address_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> addFav(long commodity_id, int type) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "add_collection");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"commodity_id\":" + commodity_id + ",\"type\":" + type + "}");
		return doServerAPI(args);
	}

	public Flowable<String> delFav(long collection_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "del_collection");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"collection_id\":" + collection_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getFav(int type) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "collection");
		args.put("interface", "{\"user_id\":" + App.mUserInfo.getUid() + ",\"type\":" + type + "}");
		return doServerAPI(args);
	}


	//index.php?action=supermarket&interface={"address_id":100009}

	public Flowable<String> getSupermarket(long address_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "supermarket");
		args.put("interface", "{\"address_id\":" + address_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getSupermarketType(long supermarket_id) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "supermarket_type");
		args.put("interface", "{\"supermarket_id\":" + supermarket_id + "}");
		return doServerAPI(args);
	}

	public Flowable<String> getSupermarketProduct(long supermarket_id, long supermarket_commodity_type, int page) {
		TreeMap<String, String> args = new TreeMap<>();
		args.put("action", "supermarket_commodity_list");
		args.put("interface", "{\"supermarket_id\":" + supermarket_id + ",\"supermarket_commodity_type\":" + supermarket_commodity_type + ",\"page\":" + page + "}");
		return doServerAPI(args);
	}

}
